<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="退货售后_id" prop="thshId">
        <el-input
          v-model="queryParams.thshId"
          placeholder="请输入退货售后_id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="快递单号" prop="trackingNumber">
        <el-input
          v-model="queryParams.trackingNumber"
          placeholder="请输入快递单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="关键字" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="退货数量" prop="returnQuantity">
        <el-input
          v-model="queryParams.returnQuantity"
          placeholder="请输入退货数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="途中是否有损坏(0:否,1:是)" prop="isDamaged">
        <el-input
          v-model="queryParams.isDamaged"
          placeholder="请输入途中是否有损坏(0:否,1:是)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="损坏数量" prop="damagedQuantity">
        <el-input
          v-model="queryParams.damagedQuantity"
          placeholder="请输入损坏数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="退货日期" prop="returnDate">
        <el-date-picker clearable
          v-model="queryParams.returnDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择退货日期">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="售后人员" prop="afterSaleStaff">
        <el-input
          v-model="queryParams.afterSaleStaff"
          placeholder="请输入售后人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="退回可用数量" prop="availableReturnQuantity">
        <el-input
          v-model="queryParams.availableReturnQuantity"
          placeholder="请输入退回可用数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户编码" prop="customerCode">
        <el-input
          v-model="queryParams.customerCode"
          placeholder="请输入客户编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料编码" prop="materialCode">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料类型" prop="materialType">
        <el-select v-model="queryParams.materialType" placeholder="请选择物料类型" clearable>
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="物料属性" prop="materialProperty">
        <el-input
          v-model="queryParams.materialProperty"
          placeholder="请输入物料属性"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属仓库" prop="warehouse">
        <el-input
          v-model="queryParams.warehouse"
          placeholder="请输入所属仓库"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="specificationModel">
        <el-input
          v-model="queryParams.specificationModel"
          placeholder="请输入规格型号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单箱" prop="bottlesPerBox">
        <el-input
          v-model="queryParams.bottlesPerBox"
          placeholder="请输入单箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属车间" prop="workshop">
        <el-input
          v-model="queryParams.workshop"
          placeholder="请输入所属车间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次号" prop="batchNumber">
        <el-input
          v-model="queryParams.batchNumber"
          placeholder="请输入批次号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:aft:retusale:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:aft:retusale:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:aft:retusale:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mes:aft:retusale:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-refresh"
          size="mini"
          @click="handleSync"
          v-hasPermi="['mes:aft:retusale:sync']"
        >数据同步</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="saleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="退货售后_id" align="center" prop="thshId" /> -->
      <el-table-column label="快递单号" align="center" prop="trackingNumber" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <!-- <el-table-column label="关键字" align="center" prop="keyword" /> -->
      <el-table-column label="产品名称" align="center" prop="productName" />
      <el-table-column label="退货数量" align="center" prop="returnQuantity" />
      <el-table-column label="单价" align="center" prop="goodsPrice" />
      <el-table-column label="总金额" align="center" prop="totalAmount" />
      <el-table-column label="途中是否有损坏" align="center" prop="isDamaged" />
      <el-table-column label="损坏数量" align="center" prop="damagedQuantity" />
      <el-table-column label="损坏原因" align="center" prop="damageReason" />
      <el-table-column label="退货日期" align="center" prop="returnDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="现场图片" align="center" prop="siteImages">
        <template slot-scope="scope">
          <el-image
            v-for="(item, index) in scope.row.siteImagesList"
            :key="index"
            :src="item"
            style="width: 30%;"
            :preview-src-list="[item]">
          </el-image>
        </template>
      </el-table-column>
      <!-- <el-table-column label="现场图片" align="center" prop="siteImages" /> -->
      <el-table-column label="售后人员" align="center" prop="afterSaleStaff" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <!-- <el-table-column label="退回可用数量" align="center" prop="availableReturnQuantity" />
      <el-table-column label="客户编码" align="center" prop="customerCode" />
      <el-table-column label="物料编码" align="center" prop="materialCode" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="物料类型" align="center" prop="materialType" />
      <el-table-column label="物料属性" align="center" prop="materialProperty" />
      <el-table-column label="所属仓库" align="center" prop="warehouse" />
      <el-table-column label="规格型号" align="center" prop="specificationModel" />
      <el-table-column label="单箱" align="center" prop="bottlesPerBox" />
      <el-table-column label="所属车间" align="center" prop="workshop" />
      <el-table-column label="批次号" align="center" prop="batchNumber" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:aft:retusale:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:aft:retusale:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退货售后对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="退货售后_id" prop="thshId">
          <el-input v-model="form.thshId" placeholder="请输入退货售后_id" />
        </el-form-item> -->
        <el-form-item label="快递单号" prop="trackingNumber">
          <el-input v-model="form.trackingNumber" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <!-- <el-form-item label="关键字" prop="keyword">
          <el-input v-model="form.keyword" placeholder="请输入关键字" />
        </el-form-item> -->
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="退货数量" prop="returnQuantity">
          <el-input v-model="form.returnQuantity" placeholder="请输入退货数量" />
        </el-form-item>
        <el-form-item label="途中是否有损坏(0:否,1:是)" prop="isDamaged">
          <el-input v-model="form.isDamaged" placeholder="请输入途中是否有损坏(0:否,1:是)" />
        </el-form-item>
        <el-form-item label="损坏数量" prop="damagedQuantity">
          <el-input v-model="form.damagedQuantity" placeholder="请输入损坏数量" />
        </el-form-item>
        <el-form-item label="损坏原因" prop="damageReason">
          <el-input v-model="form.damageReason" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="退货日期" prop="returnDate">
          <el-date-picker clearable
            v-model="form.returnDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退货日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="现场图片(存储图片路径或URL)" prop="siteImages">
          <el-input v-model="form.siteImages" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="售后人员" prop="afterSaleStaff">
          <el-input v-model="form.afterSaleStaff" placeholder="请输入售后人员" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="商品编码" prop="goodsCode">
          <el-input v-model="form.goodsCode" placeholder="请选择商品编码" readonly>
            <el-button slot="append" icon="el-icon-search" @click="selectGoods"></el-button>
          </el-input>
        </el-form-item>

        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="商品名称" readonly />
        </el-form-item>

        <el-form-item label="单价" prop="goodsPrice">
          <el-input v-model="form.goodsPrice" placeholder="单价" readonly />
        </el-form-item>

        <el-form-item label="总金额" prop="totalAmount">
          <el-input v-model="form.totalAmount" placeholder="总金额" readonly />
        </el-form-item>
        <!-- <el-form-item label="退回可用数量" prop="availableReturnQuantity">
          <el-input v-model="form.availableReturnQuantity" placeholder="请输入退回可用数量" />
        </el-form-item>
        <el-form-item label="客户编码" prop="customerCode">
          <el-input v-model="form.customerCode" placeholder="请输入客户编码" />
        </el-form-item>
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="物料类型" prop="materialType">
          <el-select v-model="form.materialType" placeholder="请选择物料类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="物料属性" prop="materialProperty">
          <el-input v-model="form.materialProperty" placeholder="请输入物料属性" />
        </el-form-item>
        <el-form-item label="所属仓库" prop="warehouse">
          <el-input v-model="form.warehouse" placeholder="请输入所属仓库" />
        </el-form-item>
        <el-form-item label="规格型号" prop="specificationModel">
          <el-input v-model="form.specificationModel" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="单箱" prop="bottlesPerBox">
          <el-input v-model="form.bottlesPerBox" placeholder="请输入单箱" />
        </el-form-item>
        <el-form-item label="所属车间" prop="workshop">
          <el-input v-model="form.workshop" placeholder="请输入所属车间" />
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input v-model="form.batchNumber" placeholder="请输入批次号" />
        </el-form-item> -->
        <!-- <el-form-item label="逻辑删除标识(0:未删除,1:已删除)" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入逻辑删除标识(0:未删除,1:已删除)" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 商品选择对话框 -->
    <el-dialog title="选择商品" :visible.sync="goodsSelectOpen" width="800px" append-to-body>
      <el-form :model="goodsQueryParams" ref="goodsQueryForm" size="small" :inline="true">
        <el-form-item label="商品编码">
          <el-input v-model="goodsQueryParams.goodsCode" placeholder="请输入商品编码" clearable />
        </el-form-item>
        <el-form-item label="商品名称">
          <el-input v-model="goodsQueryParams.goodsName" placeholder="请输入商品名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="getGoodsList">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-table
          :data="goodsList"
          @row-dblclick="confirmGoodsSelection"
          height="400"
          border>
          <el-table-column label="商品编码" align="center" prop="goodsCode" />
          <el-table-column label="商品名称" align="center" prop="goodsName" />
          <el-table-column label="单价" align="center" prop="goodsPrice">
            <template slot-scope="scope">
              {{ scope.row.goodsPrice || '0.00' }}
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="100">
            <template slot-scope="scope">
              <el-button size="mini" type="text" @click="confirmGoodsSelection(scope.row)">选择</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="goodsTotal>0"
          :total="goodsTotal"
          :page.sync="goodsQueryParams.pageNum"
          :limit.sync="goodsQueryParams.pageSize"
          @pagination="getGoodsList"
        />

        <div slot="footer" class="dialog-footer">
          <el-button @click="goodsSelectOpen = false">取 消</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
import { listSale, getSale, delSale, addSale, updateSale ,retusaleSync,listGoods} from "@/api/aft/retusale";

export default {
  name: "Sale",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退货售后表格数据
      saleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 商品选择对话框
      goodsSelectOpen: false,
      // 商品列表
      goodsList: [],
      // 商品总数
      goodsTotal: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        thshId: null,        trackingNumber: null,        brand: null,        keyword: null,        productName: null,        returnQuantity: null,        isDamaged: null,        damagedQuantity: null,        damageReason: null,        returnDate: null,        siteImages: null,        afterSaleStaff: null,        remarks: null,        availableReturnQuantity: null,        customerCode: null,        materialCode: null,        unit: null,        materialType: null,        materialProperty: null,        warehouse: null,        specificationModel: null,        bottlesPerBox: null,        workshop: null,        batchNumber: null,      },
        // 商品查询参数
            goodsQueryParams: {
              pageNum: 1,
              pageSize: 10,
              goodsCode: null,
              goodsName: null
            },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退货售后列表 */
    getList() {
      this.loading = true;
      listSale(this.queryParams).then(response => {
        this.saleList = response.rows;
        this.total = response.total;
        this.loading = false;

        this.saleList.forEach(sale => {
          let picList = [];
          let siteImages = sale.siteImages;
          if (siteImages != null && siteImages != '') {
            let siteImagesArr = siteImages.split(",");
            for (let i = 0; i < siteImagesArr.length; i++) {
              let pic = siteImagesArr[i];
              if (pic !== null && pic !== '') {
                picList.push(pic);
              }
            }
          }
          sale.siteImagesList = picList;
        });
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,        thshId: null,        trackingNumber: null,        brand: null,        keyword: null,        productName: null,        returnQuantity: null,        isDamaged: null,        damagedQuantity: null,        damageReason: null,        returnDate: null,        siteImages: null,        afterSaleStaff: null,        remarks: null,        availableReturnQuantity: null,        customerCode: null,        materialCode: null,        unit: null,        materialType: null,        materialProperty: null,        warehouse: null,        specificationModel: null,        bottlesPerBox: null,        workshop: null,        batchNumber: null,        createTime: null,        updateTime: null,        delFlag: null  ,goodsName: null,
      goodsCode: null,
      goodsPrice: null,
      totalAmount: null    };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加退货售后";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSale(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改退货售后";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSale(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSale(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除退货售后编号为"' + ids + '"的数据项？').then(function() {
        return delSale(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 数据同步按钮操作 */
    handleSync() {
      this.$modal.confirm('是否确认要同步数据').then(function() {
        return retusaleSync();
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("数据同步成功");
      }).catch(() => {});
    },
     /** 选择商品 */
      selectGoods() {
        this.goodsSelectOpen = true;
        this.getGoodsList();
      },

      /** 获取商品列表 */
      getGoodsList() {
        listGoods(this.goodsQueryParams).then(response => {
          this.goodsList = response.rows;
          this.goodsTotal = response.total;
        });
      },

      /** 选择商品确认 */
      confirmGoodsSelection(row) {
        this.form.goodsCode = row.goodsCode;
        this.form.goodsName = row.goodsName;
        this.form.goodsPrice = row.goodsPrice;
        this.calculateTotalAmount();
        this.goodsSelectOpen = false;
      },
      /** 计算总金额 */
        calculateTotalAmount() {
          if (this.form.goodsPrice && this.form.returnQuantity) {
            this.form.totalAmount = (parseFloat(this.form.goodsPrice) * parseFloat(this.form.returnQuantity)).toFixed(2);
          } else {
            this.form.totalAmount = '';
          }
        },
    /** 导出按钮操作 */
    handleExport() {
      this.download('/mes/aft/retusale/export', {
        ...this.queryParams
      }, `sale_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
